//
//  MBViewController.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 12/17/2021.
//  Copyright (c) 2021 吴奶强. All rights reserved.
//

#import "MBViewController.h"
#import "MbInitAd.h"
#import "SVProgressHUD.h"
#import "MbFlashAd.h"
#import "MbRewardAd.h"
#import "MbInterstitialAd.h"
#import "MbFullScreenAd.h"
#import "MbDrawAd.h"
#import "MbNativeAd.h"
#import "MbBannerAd.h"
#import "MBFlashViewController.h"

@interface MBViewController ()<FlashAdDelegate,RewardAdDelegate,InterstitialAdDelegate,FullScreenAdDelegate,DrawAdDelegate,NativeAdDelegate,BannerAdDelegate>

@property(nonatomic,strong)MbFlashAd *mbFlashAd;
@property(nonatomic,strong)MbRewardAd *mbRewardAd;

@property(nonatomic,strong)MbInterstitialAd *mbIntertitalAd;
@property(nonatomic,strong)MbFullScreenAd *mbFullScreenAd;
@property(nonatomic,strong)MbDrawAd *mbDrawAd;

@property(nonatomic,strong)MbNativeAd *mbNativeAd;

@property(nonatomic,strong)MbBannerAd *mbBannerAd;

@property(nonatomic,strong)UIView *drawView;

@property(nonatomic,strong)UIView *nativeView;

@property(nonatomic,strong)UIView *bannerView;
@end

@implementation MBViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self initView];
    [self initAd];
}

-(void)initView{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat space = 20;
    CGFloat widthBtn = (width - 80) / 3;
    CGFloat height = 50;
    CGFloat topY = 100;
    UIButton *initBtn = [[UIButton alloc]initWithFrame:CGRectMake(space, topY, widthBtn, height)];
    [initBtn setTitle:@"初始化" forState:UIControlStateNormal];
    [initBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [initBtn addTarget:self action:@selector(initAd) forControlEvents:UIControlEventTouchDown];
    [initBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:initBtn];
    
    UIButton *flashBtn = [[UIButton alloc]initWithFrame:CGRectMake(widthBtn + 2 * space, topY, widthBtn, height)];
    [flashBtn setTitle:@"开屏" forState:UIControlStateNormal];
    [flashBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [flashBtn addTarget:self action:@selector(flashAd) forControlEvents:UIControlEventTouchDown];
    [flashBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:flashBtn];
    
    UIButton *rewardBtn = [[UIButton alloc]initWithFrame:CGRectMake(widthBtn * 2 + 3 * space, topY, widthBtn, height)];
    [rewardBtn setTitle:@"激励视频" forState:UIControlStateNormal];
    [rewardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rewardBtn addTarget:self action:@selector(rewardAd) forControlEvents:UIControlEventTouchDown];
    [rewardBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:rewardBtn];
    
    UIButton *insertBtn = [[UIButton alloc]initWithFrame:CGRectMake(space, topY + height + space, widthBtn, height)];
    [insertBtn setTitle:@"插屏" forState:UIControlStateNormal];
    [insertBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [insertBtn addTarget:self action:@selector(insertAd) forControlEvents:UIControlEventTouchDown];
    [insertBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:insertBtn];
    
    UIButton *fullBtn = [[UIButton alloc]initWithFrame:CGRectMake(widthBtn + 2 * space, topY + height + space, widthBtn, height)];
    [fullBtn setTitle:@"全屏视频" forState:UIControlStateNormal];
    [fullBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [fullBtn addTarget:self action:@selector(fullAd) forControlEvents:UIControlEventTouchDown];
    [fullBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:fullBtn];
    
    UIButton *drawBtn = [[UIButton alloc]initWithFrame:CGRectMake(widthBtn * 2 + 3 * space, topY + height + space, widthBtn, height)];
    [drawBtn setTitle:@"draw视频" forState:UIControlStateNormal];
    [drawBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [drawBtn addTarget:self action:@selector(drawAd) forControlEvents:UIControlEventTouchDown];
    [drawBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:drawBtn];
    
    UIButton *nativeBtn = [[UIButton alloc]initWithFrame:CGRectMake(space, topY + height + space + height + space, widthBtn, height)];
    [nativeBtn setTitle:@"信息流" forState:UIControlStateNormal];
    [nativeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nativeBtn addTarget:self action:@selector(nativeAd) forControlEvents:UIControlEventTouchDown];
    [nativeBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:nativeBtn];
    
    UIButton *bannerBtn = [[UIButton alloc]initWithFrame:CGRectMake(widthBtn + 2 * space , topY + height + space + height + space, widthBtn, height)];
    [bannerBtn setTitle:@"banner" forState:UIControlStateNormal];
    [bannerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bannerBtn addTarget:self action:@selector(bannerAd) forControlEvents:UIControlEventTouchDown];
    [bannerBtn setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:bannerBtn];
    
    
    UIView *drawView = [[UIView alloc]initWithFrame:CGRectMake(0, topY + 3 * height + 3 * space   , width, screenHeight - (topY + 3 * height + 3 * space))];
    //drawView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:drawView];
    self.drawView = drawView;
    
    UIView *nativeView = [[UIView alloc]initWithFrame:CGRectMake(0, topY + 3 * height + 3 * space,0,0)];
    [self.view addSubview:nativeView];
    self.nativeView = nativeView;
    
    UIView *bannerView = [[UIView alloc]initWithFrame:CGRectMake(0, topY + 3 * height + 3 * space,300,150)];
    [self.view addSubview:bannerView];
    self.bannerView = bannerView;
    
}

-(void)initAd{

    [[MbInitAd sharedManager]initConfig:@"5209702" gdtId:@"1111485717" ksId:@"723500002" gromoreId:@"" successBlock:^{
        [SVProgressHUD showImage:nil status:@"初始化成功"];
    } errorBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD showImage:nil status:@"初始化失败"];
    }];
    
    
}

#pragma mark - 开屏请求
-(void)flashAd{
    
    MBFlashViewController *flashVC = [MBFlashViewController new];
    [flashVC setModalPresentationStyle:UIModalPresentationOverFullScreen];
    [self presentViewController:flashVC animated:NO completion:nil];
//    NSMutableArray * adItems = [NSMutableArray new];
//    AdItem * adItem = [[AdItem alloc]init];
//    [adItem setData:Pangolin codeNo:@"887425065"];
////    [adItem setData:GDT codeNo:@"7002033659199976"];
////    [adItem setData:KS codeNo:@"7235000170"];
//    //[adItem setData:GroMore codeNo:@"887574940"];
//
//
//    [adItems addObject:adItem];
//    self.mbFlashAd = [MbFlashAd new];
//    self.mbFlashAd.adItems = adItems;
//    self.mbFlashAd.rootVC = self;
//    self.mbFlashAd.delegate = self;
//    [self.mbFlashAd loadFlash];
    
}


#pragma mark - 开屏回调
- (void)splashAdDidLoad{
    [SVProgressHUD showImage:nil status:@"开屏加载成功"];
}

- (void)splashAdDidClick{
    [SVProgressHUD showImage:nil status:@"开屏点击"];
}
- (void)splashAdDidClose{
    [SVProgressHUD showImage:nil status:@"开屏结束"];
}

- (void)splashAdDidError:(NSError *)error{
    [SVProgressHUD showImage:nil status:[NSString stringWithFormat:@"%ld",error.code]];
}

#pragma mark - 激励视频
-(void)rewardAd{
    NSMutableArray * adItems = [NSMutableArray new];
    AdItem * adItem = [[AdItem alloc]init];
    [adItem setData:Pangolin codeNo:@"947613661"];
    //[adItem setData:GDT codeNo:@"9032977681417472"];
    //[adItem setData:KS codeNo:@"7235000236"];
    //[adItem setData:GroMore codeNo:@"887574940"];

    [adItems addObject:adItem];
    self.mbRewardAd = [MbRewardAd new];
    self.mbRewardAd.adItems = adItems;
    self.mbRewardAd.delegate = self;
    [self.mbRewardAd loadReward];
}

#pragma mark - 激励视频回调
- (void)rewardAdDidLoad{
    [SVProgressHUD showImage:nil status:@"激励视频加载成功"];
}
- (void)rewardAdDidDownLoadVideo{
    [SVProgressHUD showImage:nil status:@"激励视频缓冲成功"];
    [self.mbRewardAd showReward:self];
}

-(void) rewardAdDidVisible{
    [SVProgressHUD showImage:nil status:@"激励视频显示"];
}

- (void)rewardAdDidSucceed{
    [SVProgressHUD showImage:nil status:@"激励视频回报"];
}

- (void)rewardAdDidClose{
    [SVProgressHUD showImage:nil status:@"激励视频关闭"];
}

- (void)rewardAdDidError:(NSError *)error{
    [SVProgressHUD showImage:nil status:[NSString stringWithFormat:@"%ld",error.code]];
}

#pragma mark 插屏
-(void)insertAd{
    NSMutableArray * adItems = [NSMutableArray new];
    AdItem * adItem = [[AdItem alloc]init];
    //[adItem setData:Pangolin codeNo:@"946898273"];
    [adItem setData:GDT codeNo:@"6052943675896248"];
    //[adItem setData:KS codeNo:@"7235000172"];
    //[adItem setData:GroMore codeNo:@"947075025"];

    [adItems addObject:adItem];
    self.mbIntertitalAd = [MbInterstitialAd new];
    self.mbIntertitalAd.adItems = adItems;
    self.mbIntertitalAd.delegate = self;
    [self.mbIntertitalAd loadInterstitial];
}

- (void)interstitialAdDidLoad{
    [SVProgressHUD showImage:nil status:@"插屏加载完成"];
}
- (void)interstitialAdDidDownLoadVideo{
    [SVProgressHUD showImage:nil status:@"素材下载完成"];
    [self.mbIntertitalAd showInterstitial:self];
}
- (void)interstitialAdDidVisible{
    [SVProgressHUD showImage:nil status:@"插屏展示"];
}


- (void)InterstitialAdDidError:(NSError *)error{
    [SVProgressHUD showImage:nil status:error.domain];
}

#pragma mark 全屏视频
-(void)fullAd{
    NSMutableArray * adItems = [NSMutableArray new];
    AdItem * adItem = [[AdItem alloc]init];
//    [adItem setData:Pangolin codeNo:@"946898273"];
//    [adItem setData:GDT codeNo:@"2002948695898328"];
    [adItem setData:KS codeNo:@"7235000234"];
    //[adItem setData:GroMore codeNo:@"887574940"];

    
    [adItems addObject:adItem];
    self.mbFullScreenAd = [MbFullScreenAd new];
    self.mbFullScreenAd.adItems = adItems;
    self.mbFullScreenAd.delegate = self;
    [self.mbFullScreenAd loadFullScreen];
}

- (void)fullScreenAdDidLoad{
    [SVProgressHUD showImage:nil status:@"全屏加载完成"];
}
- (void)fullScreenAdDidDownLoadVideo{
    [SVProgressHUD showImage:nil status:@"素材下载完成"];
    [self.mbFullScreenAd showFullScreen:self];
}
- (void)fullScreenAdDidVisible{
    [SVProgressHUD showImage:nil status:@"全屏展示"];
}
- (void)fullScreenAdDidError:(NSError *)error{
    [SVProgressHUD showImage:nil status:error.domain];
}


#pragma mark draw视频
-(void)drawAd{
    NSMutableArray * adItems = [NSMutableArray new];
    AdItem * adItem = [[AdItem alloc]init];
    //[adItem setData:Pangolin codeNo:@"945759235"];
    [adItem setData:KS codeNo:@"7235000235"];
    
    [adItems addObject:adItem];
    self.mbDrawAd = [MbDrawAd new];
    self.mbDrawAd.adItems = adItems;
    self.mbDrawAd.rootVC = self;
    self.mbDrawAd.view = self.drawView;
    self.mbDrawAd.delegate = self;
    [self.mbDrawAd loadDraw];
}

- (void)drawAdDidError:(NSError *)error{
    [SVProgressHUD showImage:nil status:error.domain];
}

#pragma mark 信息流
-(void)nativeAd{
    NSMutableArray * adItems = [NSMutableArray new];
    AdItem * adItem = [[AdItem alloc]init];
    [adItem setData:Pangolin codeNo:@"946898241"];
//    [adItem setData:GDT codeNo:@"9032241870138275"];
    [adItem setData:KS codeNo:@"7235000171"];
    //[adItem setData:GroMore codeNo:@"946766053"];
    
    [adItems addObject:adItem];
    self.mbNativeAd = [MbNativeAd new];
    self.mbNativeAd.adItems = adItems;
    self.mbNativeAd.rootVC = self;
    self.mbNativeAd.width = 300;
    self.mbNativeAd.view = self.nativeView;
    self.mbNativeAd.delegate = self;
    [self.mbNativeAd loadNative];
}

- (void)nativeAdDidLoad:(UIView *)view{
    [SVProgressHUD showImage:nil status:@"信息流加载成功"];
}
- (void)nativeAdDislike:(UIView *)view{
    [SVProgressHUD showImage:nil status:@"信息流关闭"];
}

- (void)nativeAdDidError:(NSError *)error{
    [SVProgressHUD showImage:nil status:@"信息流加载失败"];
}

#pragma mark banner
-(void)bannerAd{
    NSMutableArray * adItems = [NSMutableArray new];
    AdItem * adItem = [[AdItem alloc]init];
//    [adItem setData:Pangolin codeNo:@"947416072"];
//    [adItem setData:GDT codeNo:@"7032573626199610"];
    //[adItem setData:GroMore codeNo:@"946766053"];
    
    [adItems addObject:adItem];
    self.mbBannerAd = [MbBannerAd new];
    self.mbBannerAd.adItems = adItems;
    //设置的尺寸务必和媒体平台配置保持相同比例
    self.mbBannerAd.width = self.bannerView.frame.size.width;
    self.mbBannerAd.height = self.bannerView.frame.size.height;
    
    self.mbBannerAd.rootVC = self;
    self.mbBannerAd.view = self.bannerView;
    self.mbBannerAd.delegate = self;
    [self.mbBannerAd loadBanner];
}

- (void)bannerAdDidLoad:(UIView *)view{
    [SVProgressHUD showImage:nil status:@"banner加载成功"];
}

- (void)bannerAdDislike:(UIView *)view{
    [SVProgressHUD showImage:nil status:@"banner不喜欢"];
}

- (void)bannerAdDidError:(NSError *)error{
    [SVProgressHUD showImage:nil status:@"banner加载失败"];
}

@end
